const path = require('path')
const { APP_PORT, SYS_LOCATION } = require('../config/config.development');
const { newsAdd, newsUpdate, newsDelete, newsDetail, newsList, getNews } = require('../service/news.service')
class NewsController {
    // 新闻图片上传
    async upload(ctx) {
        ctx.body = "图片上传成功"
        const { file } = ctx.request.files
        const fileType = ['image/jpeg', 'image/png', 'image/gif']
        if (file) {
            if (!fileType.includes(file.mimetype)) {
                return ctx.body = {
                    code: 500,
                    message: '图片格式不正确',
                    data: ""
                }
            } else {
                ctx.body = {
                    code: 200,
                    message: '图片上传成功',
                    data: {
                        url: `http://${SYS_LOCATION}:${APP_PORT}/${path.basename(file.filepath)}`
                    }
                }
            }
        } else {
            ctx.body = {
                code: 500,
                message: '图片上传失败',
                data: ""
            }
        }
    }
    // 发布新闻
    async addNews(ctx) {
        try {
            const findresult = await getNews(ctx.request.body.newsname)
            if (findresult) {
                ctx.body = {
                    code: 500,
                    message: '新闻已存在',
                    data: ""
                }
                return
            } else {
                const res = await newsAdd(ctx.request.body)
                ctx.body = {
                    code: 200,
                    message: '添加成功!',
                    data: res
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '添加失败!',
                data: err
            }
            return
        }
    }
    // 修改新闻
    async updateNews(ctx) {
        try {
            const res = await newsUpdate(ctx.request.body)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '更新成功!',
                    data: ''
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '更新失败!',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '更新失败!',
                data: err
            }
            return
        }
    }
    // 删除新闻
    async deleteNews(ctx) {
        try {
            const res = await newsDelete(ctx.request.body.id)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '删除成功!',
                    data: ''
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '删除失败!',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '删除失败!',
                data: err
            }
        }
    }
    // 获取详情
    async getNewsDetail(ctx) {
        try {
            const res = await newsDetail(ctx.request.body.id)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '获取详情成功!',
                    data: res
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '获取详情失败!',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '获取详情失败!',
                data: err
            }
        }
    }
    // 获取列表
    async getNewsList(ctx) {
        try {
            const { res, total } = await newsList(ctx.request.body.page, ctx.request.body.pageSize, ctx.request.body)
            if (res) {
                ctx.body = {
                    code: 200,
                    message: '获取列表成功!',
                    data: res,
                    total: total
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: '获取列表失败!',
                    data: ""
                }
            }
        } catch (err) {
            ctx.body = {
                code: 500,
                message: '获取列表失败!',
                data: err
            }
        }
    }
}
module.exports = new NewsController()